<?php
/**
 * 联合图表
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    Farmer.Li <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   SVN: $Id$
 */
namespace Project\Apps\Ecga\Library\FusionCharts;

use \Project\Apps\Ecga\Library\FusionCharts\ChartXml;
use \Exception;

/**
 * 联合图表
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    Farmer.Li <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: <0.2>
 */
class Combination extends ChartXml
{
	/**
	 * 构造
	 * 
	 * @param string $caption    表说明
	 * @param string $pYAxisName 左Y轴
	 * @param string $sYAxisName 右Y轴
	 * 
	 * @return void
	 */
	public function __construct($caption, $pYAxisName, $sYAxisName)
	{
		parent::__construct($caption);
		$this->setStyle(
			array(
				'pYAxisName' => $pYAxisName,
				'sYAxisName' => $sYAxisName,
				'sNumberSuffix' => '%',
				'showValues' => 0
			)
		);
	}
	
	/**
	 * 设置数据
	 * 
	 * @param string $pName 左纵轴名称
	 * @param array  $pData 左纵轴数据
	 * @param array  $sData 右纵轴数据
	 * 
	 * @return void
	 */
	public function setData($pName, $pData = array(), $sData = array())
	{
		$categories = array(
			'tagName' => 'categories',
			'__child' => array()
		);
		foreach ($pData as $key => $value) {
			$categories['__child'][] = array(
				'tagName' => 'category',
				'__attr' => array(
					'label' => $key
				)
			);
		}
		$this->data['__child']['categories'] = $categories;
		$this->data['__child'][] = $this->_getDataSet($pName, '', $pData);
		foreach ($sData as $key => $data) {
			$this->data['__child'][] = $this->_getDataSet($key, 'S', $data);
		}
	}
	
	/**
	 * 生成dataSet
	 * 
	 * @param string $seriesName  数据名称
	 * @param string $parentYAxis 指定数据所属轴
	 * @param array  $data        数据
	 * 
	 * @return void
	 */
	private function _getDataSet($seriesName, $parentYAxis, $data)
	{
		$result = array(
			'tagName' => 'dataset',
			'__child' => array()
		);
		$result['__attr']['seriesName'] = $seriesName;
		if ($parentYAxis != '') {
			$result['__attr']['parentYAxis'] = $parentYAxis;
		}
		foreach ($data as $key => $value) {
			$result['__child'][] = array(
				'tagName' => 'set',
				'__attr' => array(
					'value' => $value
				)
			);
		}
		return $result;
	}
}
?>